
package com.feib.soeasy.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;



import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.DateTypeConverter;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.Soez5008Service;
import com.feib.soeasy.util.UserUtil;

/**
 * @title (#)Soez5008ActionBean.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/3/19 create by Jimmy Liu<br>
 */
public class Soez5008ActionBean extends BaseActionBean {


	private static final String DEFAULT_JSP = "/WEB-INF/views/soez5008/soez5008.jsp";

	
	private static final String AJAX_LIST_DETAIL = "/WEB-INF/views/soez5008/soez5008DetailList.jsp";


	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFrom;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateTo;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromD;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToD;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromE;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToE;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromF;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToF;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromG;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToG;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromH;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToH;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromAll;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToAll;


	/**
	 * 查詢條件
	 * 帳單業者
	 */
	private Group qryGroup;

	private String queryChannel;

	private String queryCommissionCode;


	private String channelD;
	private String channelE;
	private String channelF;
	private String channelG;
	private String channelH;

	/**
	 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
	 *detailType =1.單通路單代碼 ex: 7-11  61D 
	 *detailType=2.單通路全代碼 ex: 7-11  全部代碼
	 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
	 */
	private String detailType;



	@SpringBean("soez5008Service")
	private Soez5008Service soez5008Service;




	/**
	 * 查詢結果
	 */
	private PaginatedList<Map<String, Object>> queryResult;

	

	@DontBind
	@DefaultHandler
	public Resolution doDefault()
	{
		logger.debug("Enter doDefault mehtod.");
		try {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -1);

			qryDateToD = cal.getTime();
			qryDateFromD = cal.getTime();

			qryDateToE = cal.getTime();
			qryDateFromE = cal.getTime();

			qryDateToF = cal.getTime();
			qryDateFromF = cal.getTime();

			qryDateToG = cal.getTime();
			qryDateFromG = cal.getTime();

			qryDateToH = cal.getTime();
			qryDateFromH = cal.getTime();

			/* 全部勾選  */
			qryDateToAll = cal.getTime();
			qryDateFromAll = cal.getTime();

		}
		catch (Exception e) {
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}

	public Resolution doAjaxQuery()
	{
		logger.debug("Enter doAjaxQuery mehtod.");
		try {
			String[] channels = {
					channelD,channelE,channelF ,channelG, channelH};

			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};
			
			/* 限制卷商查詢 */
			Group	group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}
			/*
			 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
			 */
			String message = this.soez5008Service.doExportGroupStatisticsHtml(group, channels, qryDateFroms, qryDateTos);

			StreamingResolution resolution = new StreamingResolution("text/html", new StringReader(message.toString()));
			resolution.setCharacterEncoding("UTF-8");
			return resolution;


		}
		catch (Exception e) {
			logger.error("超商代收統計表(月)錯誤(Soez5008ActionBean.doAjaxQuery): {}",e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}

	}



	public Resolution doDownloadDetailExcelFile()
	{
		logger.debug("Enter doDownloadDetailExcelFile mehtod.");
		try {


			//如果不是銀行端，則查詢條件要加入帳單業者限制
			/* 限制卷商查詢 */
			Group	group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}
			/*
			 *區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType =1.單通路單代碼 ex: 7-11  61D 
			 *detailType=2.單通路全代碼 ex: 7-11  全部代碼
			 */
			String downloadFilePath = soez5008Service.doAjaxQueryGroupCommissionCodeXls(group,queryChannel, queryCommissionCode, qryDateFrom, qryDateTo);
			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();
			String isoFileName = new String("超商傳輸統計明細表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}



	/**
	 * 下載畫面Excel
	 * @return
	 */
	public Resolution doAjaxDownloadExcelFile()
	{
		logger.debug("Enter doAjaxDownloadExcelFile mehtod.");
		try {
			String[] channels = {
					channelD,channelE,channelF ,channelG, channelH};

			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};



			/* 限制卷商查詢 */
			Group	group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			/*
			 *區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
			 */
			String downloadFilePath = this.soez5008Service.doExportGroupStatisticsXls(group,channels,  qryDateFroms , qryDateTos);

			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("超商傳輸統計表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}


	/**
	 * 根據通路跟日期跟代號 取得  CommissionCode 明細 (可分群組)
	 * @author Jeff
	 */
	public Resolution doAjaxQueryCommissionCode()
	{
		logger.debug("Enter doAjaxQueryCommissionCode mehtod.");
		try {

			/* 區分需要觀察的明細類型
	 		   1.單通路單代碼 ex: 7-11  61D 
			 */		

			/* 不是帳單業者要加入限制 */
			Group group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group= UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();
			/* 單通路單代碼明細資料保持顯示全部不分頁*/
			queryResult.setPageSize(-1);

			queryResult = soez5008Service.queryGroupFeedbackLogByChannelCommission(group,queryChannel, queryCommissionCode, qryDateFrom, qryDateTo,queryResult);


		}
		catch (Exception e) {
			logger.error("超商代收統計查詢錯誤(Soez5008ActionBean.doAjaxQueryCommissionCode): {}",e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQueryCommissionCode.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}
		return new ForwardResolution(AJAX_LIST_DETAIL); 
	}


	/**
	 * 
	 * 根據通路跟日期 取得  CommissionCode 明細 (可分群組)
	 * @author Jeff
	 */
	public Resolution doAjaxQueryTotalCommissionCode()
	{
		logger.debug("Enter doAjaxQueryTotalCommissionCode mehtod.");
		try {

			/* 不是帳單業者要加入限制 */
			Group group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group= UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();

			/**
			 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType=2.單通路全代碼 ex: 7-11  全部代碼
			 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
			 */

			if("3".equals(detailType)){
				String[] channels = {
						channelD,channelE,channelF ,channelG, channelH};

				Date [] qryDateFroms = 
				{
						qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
				};

				Date[] qryDateTos =
				{
						qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
				};

				queryResult = soez5008Service.queryGroupFeedbackLogByChannel(group,channels, qryDateFroms, qryDateTos,queryResult);
			}else if("2".equals(detailType)){

				String[] channels = {queryChannel};

				Date [] qryDateFroms = {qryDateFrom};

				Date[] qryDateTos ={qryDateTo};

				queryResult = soez5008Service.queryGroupFeedbackLogByChannel(group,channels, qryDateFroms, qryDateTos,queryResult);

			}



		}
		catch (Exception e) {
			logger.error("超商代收統計查詢錯誤(Soez5008ActionBean.doAjaxQueryTotalCommissionCode): {}",e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQueryTotalCommissionCode.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}
		return new ForwardResolution(AJAX_LIST_DETAIL); 
	}



	/**
	 * 下載畫面Excel
	 * @return
	 */
	public Resolution doAjaxDownloadTotalExcelFile()
	{
		logger.debug("Enter doAjaxDownloadTotalExcelFile mehtod.");
		try {
			String[] channels = {
					channelD,channelE,channelF ,channelG, channelH};

			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};



			/* 限制卷商查詢 */
			Group	group=new Group();
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			/*
			 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
			 */
			String downloadFilePath = this.soez5008Service.doExportGroupDetailStatisticsXls(group,channels,  qryDateFroms , qryDateTos);

			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("超商傳輸統計表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}




	/**
	 * @return the qryDateFrom
	 */
	public Date getQryDateFrom() {
		return qryDateFrom;
	}


	/**
	 * @param qryDateFrom the qryDateFrom to set
	 */
	public void setQryDateFrom(Date qryDateFrom) {
		this.qryDateFrom = qryDateFrom;
	}


	/**
	 * @return the qryDateTo 
	 */
	public Date getQryDateTo() {
		return qryDateTo;
	}


	/**
	 * @param qryDateTo the qryDateTo to set
	 */
	public void setQryDateTo(Date qryDateTo) {
		this.qryDateTo = qryDateTo;
	}


	/**
	 * @return the qryGroup
	 */
	public Group getQryGroup() {
		return qryGroup;
	}


	/**
	 * @param qryGroup the qryGroup to set
	 */
	public void setQryGroup(Group qryGroup) {
		this.qryGroup = qryGroup;
	}


	public String getQueryChannel() {
		return queryChannel;
	}

	public void setQueryChannel(String queryChannel) {
		this.queryChannel = queryChannel;
	}

	public String getQueryCommissionCode() {
		return queryCommissionCode;
	}

	public void setQueryCommissionCode(String queryCommissionCode) {
		this.queryCommissionCode = queryCommissionCode;
	}


	public Date getQryDateFromD() {
		return qryDateFromD;
	}

	public void setQryDateFromD(Date qryDateFromD) {
		this.qryDateFromD = qryDateFromD;
	}

	public Date getQryDateToD() {
		return qryDateToD;
	}

	public void setQryDateToD(Date qryDateToD) {
		this.qryDateToD = qryDateToD;
	}

	public Date getQryDateFromE() {
		return qryDateFromE;
	}

	public void setQryDateFromE(Date qryDateFromE) {
		this.qryDateFromE = qryDateFromE;
	}

	public Date getQryDateToE() {
		return qryDateToE;
	}

	public void setQryDateToE(Date qryDateToE) {
		this.qryDateToE = qryDateToE;
	}

	public Date getQryDateFromF() {
		return qryDateFromF;
	}

	public void setQryDateFromF(Date qryDateFromF) {
		this.qryDateFromF = qryDateFromF;
	}

	public Date getQryDateToF() {
		return qryDateToF;
	}

	public void setQryDateToF(Date qryDateToF) {
		this.qryDateToF = qryDateToF;
	}

	public Date getQryDateFromG() {
		return qryDateFromG;
	}

	public void setQryDateFromG(Date qryDateFromG) {
		this.qryDateFromG = qryDateFromG;
	}

	public Date getQryDateToG() {
		return qryDateToG;
	}

	public void setQryDateToG(Date qryDateToG) {
		this.qryDateToG = qryDateToG;
	}

	public Date getQryDateFromH() {
		return qryDateFromH;
	}

	public void setQryDateFromH(Date qryDateFromH) {
		this.qryDateFromH = qryDateFromH;
	}

	public Date getQryDateToH() {
		return qryDateToH;
	}

	public void setQryDateToH(Date qryDateToH) {
		this.qryDateToH = qryDateToH;
	}

	public Date getQryDateFromAll() {
		return qryDateFromAll;
	}

	public void setQryDateFromAll(Date qryDateFromAll) {
		this.qryDateFromAll = qryDateFromAll;
	}

	public Date getQryDateToAll() {
		return qryDateToAll;
	}

	public void setQryDateToAll(Date qryDateToAll) {
		this.qryDateToAll = qryDateToAll;
	}


	public String getChannelD() {
		return channelD;
	}

	public void setChannelD(String channelD) {
		this.channelD = channelD;
	}

	public String getChannelE() {
		return channelE;
	}

	public void setChannelE(String channelE) {
		this.channelE = channelE;
	}

	public String getChannelF() {
		return channelF;
	}

	public void setChannelF(String channelF) {
		this.channelF = channelF;
	}

	public String getChannelG() {
		return channelG;
	}

	public void setChannelG(String channelG) {
		this.channelG = channelG;
	}

	public String getChannelH() {
		return channelH;
	}

	public void setChannelH(String channelH) {
		this.channelH = channelH;
	}

	public String getDetailType() {
		return detailType;
	}

	public void setDetailType(String detailType) {
		this.detailType = detailType;
	}
	
	public PaginatedList<Map<String, Object>> getQueryResult() {
		return queryResult;
	}

	public void setQueryResult(PaginatedList<Map<String, Object>> queryResult) {
		this.queryResult = queryResult;
	}
}
